package com.song.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.entity.UserInfo;
import com.song.entity.vo.*;
import com.song.exception.BusinessException;
import com.song.result.Result;
import com.song.service.UserInfoService;
import com.song.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Song
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    // http://localhost/api/core/userInfo/register   post

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("会员注册")  //我这边带着你们做借款人注册  寒假的时候 你们做 投资人注册
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVO registerVO){

        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //从redis中获取验证码
        String codeMsg = stringRedisTemplate.opsForValue().get("duanxin:" + mobile);
        //判断是否相同

        userInfoService.register(registerVO);

        return Result.ok().message("注册成功了....");

    }

    // http://localhost/api/core/userInfo/login
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO, HttpServletRequest request ) throws BusinessException {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Integer userType = loginVO.getUserType();
        //StringUtils.isEmpty() 自己判断去

        //获取ip
        String ip = request.getRemoteAddr();

        UserInfoVO userInfoVO = userInfoService.login(loginVO,ip);

        return Result.ok().data("userInfo",userInfoVO);

    }


    // /api/core/userInfo/auth/getIndexUserInfo

    @ApiOperation("会员登录")
    @GetMapping("/auth/getIndexUserInfo")
    public Result getIndexUserInfo(HttpServletRequest request ){
         //通过请求头获取 token 信息
        String token = request.getHeader("token");
        //获取用户的 id
        Long userId = JwtUtils.getUserId(token);

       UserIndexVO uiv= userInfoService.getUserIndexVoByUserId(userId);

        return Result.ok().data("userIndexVO",uiv);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public Result checkToken(HttpServletRequest request){
        //通过请求头获取 token 信息
        String token = request.getHeader("token");
        //获取用户的 id
        boolean result = JwtUtils.checkToken(token);

        if(result){
              return  Result.ok();
        }else{
            return  Result.error();
        }

    }


  //  http://localhost/admin/core/userInfo/list/1/10
    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result listPage(
            @ApiParam(value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象",required = false)
             UserInfoQuery userInfoQuery
            ){
          //mp中的分页类
        Page<UserInfo> pages = new Page<>(page,limit);

        IPage<UserInfo> infoIPage= userInfoService.listPage(pages,userInfoQuery);

        return Result.ok().data("pageModel",infoIPage);
    }


}

